/**
 * @class   wosh::gui::NavigatorButton
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: Navigator.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
#ifndef __WOSH_Gui_Navigator_H__
 #define __WOSH_Gui_Navigator_H__

#include <framework/gui/kiosk/KioskSheet.h>

#include <QtGui/QGraphicsRectItem>
#include <QtGui/QGraphicsWidget>
#include <QtGui/QPainter>
#include <QStateMachine>
#include <QPropertyAnimation>


namespace wosh {
 namespace gui {


class NavigatorButton : public QGraphicsWidget, public NavigatorItem  {

	Q_OBJECT

	public:
		NavigatorButton(const QString& imageStr, QGraphicsItem *parent = 0) : QGraphicsWidget(parent) {
			QPixmap pix( QString(":/%1.png").arg(imageStr));
			this->orig = pix;
			this->p = pix;

			setObjectName(imageStr);
		}

		void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
		{
			painter->drawPixmap(QPointF(), p);
		}

		virtual void mousePressEvent(QGraphicsSceneMouseEvent * )
		{
			emit clicked();
		}

		virtual void setGeometry(const QRectF &rect)
		{
			QGraphicsWidget::setGeometry(rect);

	//		if (rect.size().width() > orig.size().width())
				p = orig.scaled(rect.size().toSize());
	//		else
	//			p = orig;
		}

	Q_SIGNALS:
		void clicked();

	private:
		QPixmap orig;
		QPixmap p;

};

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

class Navigator : public QObject, public QGraphicsRectItem  {

    Q_OBJECT

	public:
		Navigator() : QGraphicsRectItem() {
			this->itemCurrent = NULL;
			this->itemStandBy = NULL;

			setBrush( Qt::NoBrush );
			setPen( Qt::NoPen );

			setFlag(QGraphicsItem::ItemIsMovable,false);
			setFlag(QGraphicsItem::ItemIsFocusable,false);
			setFlag(QGraphicsItem::ItemIsSelectable,false);

			setupBackground();
		 }
		virtual ~Navigator() { }


	public:

		void setupBackground() {
			QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
			const QColor col = QColor(51, 153, 255, 255);
			gradient.setColorAt(0, col);
			gradient.setColorAt(1, col.dark(int(200 + 0 * 50)));
			setBrush(gradient);
		 }

		void buttonsCreate() {
			this->itemHome = new NavigatorButton("styles/default/wosh.application.SheetHome", this);
			this->itemHome->context = "KiosK::Sheet::Home";

			this->itemStandBy = new NavigatorButton("styles/default/StandBy", this);
			this->itemStandBy->context = "KiosK::Command::StandBy";

			NavigatorButton* p2 = new NavigatorButton("styles/default/wosh.application.SheetAdmin", this);

			NavigatorButton* p3 = new NavigatorButton("styles/default/wosh.gui.FormConsole", this);
			NavigatorButton* p4 = new NavigatorButton("styles/default/wosh.gui.FormPlayerAudio", this);

			p2->context = "KiosK::Sheet::Admin";
			p3->sheet_name = "KiosK::Sheet::Network";
			p4->sheet_name = "sheet4";

			this->items << p2 << p3 << p4 << this->itemHome << this->itemStandBy;

			for(int i=0; i<this->items.size(); i++) {
				connect(this->items.at(i), SIGNAL(clicked()), this, SLOT(button_clicked()) );
			 }
		 }

		void setupButtons() {

			buttonsCreate();

			qreal btnWidth = 64;
			qreal btnHeigth = 64;

			QPointF oddst = this->rect().center();
			qreal top = oddst.y() - btnHeigth/2 + 10;
			qreal left = 60;

			for(int i=0; i<this->items.size(); i++) {
				if ( this->items.at(i) == NULL ) continue;
				if ( this->items.at(i) == this->itemStandBy ) continue;
				if ( this->items.at(i) == this->itemHome ) continue;
				this->items.at(i)->setGeometry(QRectF(left+i*(btnWidth+60), top, btnWidth, btnHeigth));
				this->items.at(i)->setZValue( this->zValue() +1 );
			 }
			this->itemStandBy->setGeometry(QRectF(this->rect().width() - 2*(btnWidth+30), top, btnWidth, btnHeigth));
			this->itemHome->setGeometry(QRectF(this->rect().width() - (btnWidth+30), top, btnWidth, btnHeigth));

#if QT_VERSION >= 0x040600
			setupAnimation();
#endif
		}

#if QT_VERSION >= 0x040600
		void setupAnimation() {
			machine.setGlobalRestorePolicy(QStateMachine::RestoreProperties);

			QState *group = new QState(&machine);
			group->setObjectName("group");

			QState *idleState = new QState(group);
			group->setInitialState(idleState);


			for(int i=0; i<this->items.size(); i++) {
				if ( this->items.at(i) == NULL ) continue;
				if ( this->items.at(i) == this->itemStandBy ) continue;

				QRect selectedRect = this->items.at(i)->geometry().adjusted(-32, -48, +32, +16).toRect();
				//QRect selectedRect(oddst.x()-64, oddst.y()-64, 128, 128);

				QState *state = new QState(group);
				state->assignProperty(this->items.at(i), "geometry", selectedRect);
				group->addTransition(this->items.at(i), SIGNAL(clicked()), state);
				machine.addDefaultAnimation(new QPropertyAnimation(this->items.at(i), "geometry"));    
			 }

			machine.setInitialState(group);
			machine.start();
		 }
#endif

	public:
		NavigatorButton* getItemCurrent()				{ return this->itemCurrent; }
		QList<NavigatorButton*>& getItems()				{ return this->items; }

	protected slots:
		void button_clicked() {
			NavigatorButton* sender = dynamic_cast<NavigatorButton*>(  QObject::sender() );
			this->itemCurrent = sender;
			emit itemSelected( this->itemCurrent );
		 }

	Q_SIGNALS:
		void itemSelected( NavigatorItem* item );

	protected:
		QList<NavigatorButton*> items;
		NavigatorButton* itemCurrent;
		NavigatorButton* itemStandBy;
		NavigatorButton* itemHome;

#if QT_VERSION >= 0x040600
	protected:
		QStateMachine machine;
#endif

};

  }; // namespace gui
}; // namespace wosh

#endif //__WOSH_Gui_Navigator_H__
